package com.adcc.ags.gms.gw.controller;

import com.adcc.ags.gms.core.util.Constant;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.core.vo.ResultState;
import com.adcc.ags.gms.core.vo.ResultVO;
import com.adcc.ags.gms.gw.service.SysConfigurationService;
import com.google.common.base.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 告警配置控制
 */
@Controller
@RequestMapping(value="/sysConfiguration",produces = MediaType.APPLICATION_JSON_VALUE)
public class SysConfigurationController {

    @Autowired
    private SysConfigurationService sysConfigurationService;

    @Autowired
    private LogFactory log;

    @RequestMapping(value = "/getSysConfigs", method = RequestMethod.GET)
    public @ResponseBody ResultVO getSysConfigs() {
        ResultVO resultVO = new ResultVO();
        try{
            Optional<Map<String,List<Map<String, String>>>> optional = sysConfigurationService.getSysConfigs();
            if (optional.isPresent()){
                resultVO.setData(optional.get());
            } else {
                resultVO.setCode(-1);
                resultVO.setState(ResultState.FAILURE);
                resultVO.setReason(Constant.GMS_DB_ERROR);
            }
        } catch (Exception ex){
            log.error(SysConfigurationController.class, "getSysConfigs() error", ex);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason(Constant.GMS_SERVER_ERROR);
        }
        return resultVO;
    }

    @RequestMapping(value ="/updateSysConfig",method = RequestMethod.POST)
    public @ResponseBody ResultVO updateSysConfig(String sysArray) {
        ResultVO resultVO = new ResultVO();
        try {
            Optional<Boolean> optional = sysConfigurationService.updateSysConfig(sysArray);
            if (!optional.isPresent()) {
                resultVO.setCode(-1);
                resultVO.setState(ResultState.FAILURE);
                resultVO.setReason(Constant.GMS_DB_ERROR);
            }
        } catch (Exception e) {
            log.error(SysConfigurationController.class, "updateSysConfig() error", e);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason(Constant.GMS_SERVER_ERROR);
        }
        return resultVO;
    }

}
